package com.knowif.pojo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.knowif.enumInfo.InfoStatusEnum;
import com.knowif.util.commonUtil.DateUtils;

@Validated
public class UserComment {
	private Integer id;
	// 评论内容
	@NotNull(message = "评论内容不能为空")
	@Length(max = 200)
	private String content;
	//创建时间
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private Date createTime;
	 //上级评论id
	private Integer parentId;
	//用户id
	@NotNull(message = "用户id不能为空")
	private Integer userId;
	@NotNull(message = "导师id不能为空")
	private Integer tutorId;
	//订单id
	@NotNull(message = "订单id不能为空")
	private Integer userOrderId;
	//状态
	private Integer status;
	private List<CommentPicture> commentPictures;
	private List<UserCommentLabel> commentLabels;
	private Integer helpfulCount;

	private TUser tUser;
	private String time;
	private int star;
	//是否有用  如果为0则是谢谢，如果为1则是灰色的有用
	private int isUsed;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content == null ? null : content.trim();
	}

	public int getStar() {
		return star;
	}

	public void setStar(int star) {
		this.star = star;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public Integer getUserOrderId() {
		return userOrderId;
	}

	public void setUserOrderId(Integer userOrderId) {
		this.userOrderId = userOrderId;
	}

	public Integer getStatus() {
		if (status == null) {
			status = InfoStatusEnum.ISOK.getStatus();
		}
		return status;
	}
	
	public int getIsUsed() {
		return isUsed;
	}

	public void setIsUsed(int isUsed) {
		this.isUsed = isUsed;
	}

	public String getTime() {
		if(createTime != null){
			time = DateUtils.formatDateTime1(createTime).substring(0, 10);
		}
		return time;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}

	public List<CommentPicture> getCommentPictures() {
		return commentPictures;
	}

	public void setCommentPictures(List<CommentPicture> commentPictures) {
		this.commentPictures = commentPictures;
	}
	
	public Integer getTutorId() {
		return tutorId;
	}

	public void setTutorId(Integer tutorId) {
		this.tutorId = tutorId;
	}

	public Integer getHelpfulCount() {
		if(helpfulCount == null){
			helpfulCount = 0;
		}
		return helpfulCount;
	}

	public void setHelpfulCount(Integer helpfulCount) {
		this.helpfulCount = helpfulCount;
	}

	public TUser gettUser() {
		return tUser;
	}

	public void settUser(TUser tUser) {
		this.tUser = tUser;
	}
	
	public List<UserCommentLabel> getCommentLabels() {
		return commentLabels;
	}

	public void setCommentLabels(List<UserCommentLabel> commentLabels) {
		this.commentLabels = commentLabels;
	}

	public static List<Integer> getIds(List<UserComment> userComments) {
		if (userComments == null || userComments.isEmpty()) {
			return new ArrayList<Integer>();
		}
		List<Integer> ids = new ArrayList<Integer>();
		for (UserComment userComment : userComments) {
			ids.add(userComment.getId());
		}
		return ids;
	}
	
	
	public static List<Integer> getOrderIds(List<UserComment> userComments) {
		if (userComments == null || userComments.isEmpty()) {
			return new ArrayList<Integer>();
		}
		List<Integer> orderIds = new ArrayList<Integer>();
		for (UserComment userComment : userComments) {
			orderIds.add(userComment.getUserOrderId());
		}
		return orderIds;
	}
	

	
	public static List<UserComment> saveTUsers(List<UserComment> userComments,List<TUser> tUsers){
		if(userComments == null || userComments.size() == 0){
			return new ArrayList<UserComment>();
		}
		for(UserComment userComment : userComments){
			tUsers:{
				for(int i = 0;tUsers != null && i<tUsers.size();i++){
					if(userComment.getUserId().equals(tUsers.get(i).getId())){
						userComment.settUser(tUsers.get(i));
						break tUsers;
					}
				}
			}
		}
		return userComments;
		
	}
	
	public static List<UserOrder> saveTopics(List<Topic> topics,List<UserOrder> userOrders){
		if(userOrders == null || userOrders.isEmpty()){
			return new ArrayList<UserOrder>();
		}
		for(UserOrder userOrder : userOrders){
			for(int i = 0;topics != null && i<topics.size();i++){
				if(userOrder.getTopicId() != null && userOrder.getTopicId().equals(topics.get(i).getId())){
					userOrder.setTopic(topics.get(i));
				}
			}
		}
		return userOrders;
		
	}
	
	public static List<UserComment> saveUserCommentLabel(List<UserComment> userComments,List<UserCommentLabel> userCommentLabels){
		if(userComments == null){
			return new ArrayList<UserComment>();
		}
		if(userCommentLabels == null || userCommentLabels.isEmpty()){
			return userComments;
		}
		List<UserCommentLabel> commentLabels = null;
		for(UserComment userComment : userComments){
			commentLabels = new ArrayList<UserCommentLabel>();
			for(int i = 0;userCommentLabels != null && i<userCommentLabels.size();i++){
				if(userComment.getId().equals(userCommentLabels.get(i).getUserCommentId())){
					commentLabels.add(userCommentLabels.get(i));
				}
			}
			userComment.setCommentLabels(commentLabels);
		}
		return userComments;
	}
	

	/**
	 * @author: xingyao 
	 * @Description: 把评论图片放在评论里面 
	 * @param userComments
	 * @param commentPictures
	 * @return
	 */
	public static List<UserComment> savePictures(List<UserComment> userComments,List<CommentPicture> commentPictures){
		if(userComments == null || userComments.isEmpty()){
			return new ArrayList<UserComment>();
		}
		
		List<CommentPicture> pictures = null;
		for(UserComment userComment : userComments){
			pictures = new ArrayList<CommentPicture>();
			for(int i = 0;commentPictures != null && i<commentPictures.size();i++){
				if(userComment.getId().equals(commentPictures.get(i).getOrderCommentId())){
					pictures.add(commentPictures.get(i));
				}
			}
			userComment.setCommentPictures(pictures);
		}
		return userComments;
	}
	
	public static List<Integer> getUserIds(List<UserComment> userComments){
		if(userComments == null){
			return new ArrayList<Integer>();
		}
		List<Integer> userIds = new ArrayList<Integer>();
		for(UserComment userComment : userComments){
			userIds.add(userComment.getUserId());
		}
		return userIds;
	}
	
	
	   /**
     * id进行排序
     */
    public static Comparator<UserComment> userCommentComparator = new Comparator<UserComment>() {
        public int compare(UserComment u1, UserComment u2) {
            return u1.getId() - u2.getId();
        }
    };
	
    /**
     * @author: xingyao 
     * @Description:  得到userComments对面里面的ids
     * @param userComments
     * @return
     */
	public static List<Integer> getCommendIds(List<UserComment> userComments){
		if(userComments == null){
			return null;
		}
		List<Integer> userCommentIds = new ArrayList<Integer>();
		for(UserComment userComment : userComments){
			userCommentIds.add(userComment.getId());
		}
		return userCommentIds;
	}
	
	
	public static void saveHelpful(Integer userId, List<UserComment> userComments, List<UserHelpful> userHelpfuls) {
		if(userComments == null || userComments.isEmpty() || userHelpfuls == null || userHelpfuls.isEmpty()){
			return;
		}
		for(UserHelpful userHelpful : userHelpfuls){
			for(UserComment userComment : userComments){
				if(userHelpful.getUserId().equals(userId) && userHelpful.getUserCommentId().equals(userComment.getId())){
					userComment.setIsUsed(InfoStatusEnum.ISOK.getStatus());
				}
			}
		}
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((commentPictures == null) ? 0 : commentPictures.hashCode());
		result = prime * result + ((content == null) ? 0 : content.hashCode());
		result = prime * result + ((createTime == null) ? 0 : createTime.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((parentId == null) ? 0 : parentId.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		result = prime * result + ((tUser == null) ? 0 : tUser.hashCode());
		result = prime * result + ((time == null) ? 0 : time.hashCode());
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		result = prime * result + ((userOrderId == null) ? 0 : userOrderId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserComment other = (UserComment) obj;
		if (commentPictures == null) {
			if (other.commentPictures != null)
				return false;
		} else if (!commentPictures.equals(other.commentPictures))
			return false;
		if (content == null) {
			if (other.content != null)
				return false;
		} else if (!content.equals(other.content))
			return false;
		if (createTime == null) {
			if (other.createTime != null)
				return false;
		} else if (!createTime.equals(other.createTime))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (parentId == null) {
			if (other.parentId != null)
				return false;
		} else if (!parentId.equals(other.parentId))
			return false;
		if (status == null) {
			if (other.status != null)
				return false;
		} else if (!status.equals(other.status))
			return false;
		if (tUser == null) {
			if (other.tUser != null)
				return false;
		} else if (!tUser.equals(other.tUser))
			return false;
		if (time == null) {
			if (other.time != null)
				return false;
		} else if (!time.equals(other.time))
			return false;
		if (userId == null) {
			if (other.userId != null)
				return false;
		} else if (!userId.equals(other.userId))
			return false;
		if (userOrderId == null) {
			if (other.userOrderId != null)
				return false;
		} else if (!userOrderId.equals(other.userOrderId))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "UserComment [id=" + id + ", content=" + content + ", createTime=" + createTime + ", parentId="
				+ parentId + ", userId=" + userId + ", tutorId=" + tutorId + ", userOrderId=" + userOrderId
				+ ", status=" + status + ", commentPictures=" + commentPictures + ", commentLabels=" + commentLabels
				+ ", helpfulCount=" + helpfulCount + ", tUser=" + tUser + ", time=" + time + ", star=" + star
				+ ", isUsed=" + isUsed + "]";
	}
}